/*
* $Id: AddHousingDialog.cpp 165 2010-10-14 18:26:07Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Alexey Volokitin
* All rights reserved
*
*/
#include "Common/include.h"
#include "AddHousingDialog.h"
#include "Components/Journal/HousingManager.h"

CAddHousingDialog::CAddHousingDialog(CWnd* pParent)
: CDialog(IDD_ADD_HOUSING, pParent){
	m_pFont = CCommonFunction::GetFont(_T("MS Sans Serif"), 14, false, false);
	m_oldSel = 2;
}
CAddHousingDialog::~CAddHousingDialog(){
	m_pFont->DeleteObject();
	delete m_pFont;
}

BOOL CAddHousingDialog::OnInitDialog(){
	BOOL bInit = __super::OnInitDialog();
	SetWindowText(ILanguageManager::GetLanguageManager()->GetString(ADD_HOUSING_ID).c_str());
	SetDlgItemText(IDOK, ILanguageManager::GetLanguageManager()->GetString(ADD_ID).c_str());
	SetDlgItemText(IDCANCEL, ILanguageManager::GetLanguageManager()->GetString(CANCEL_ID).c_str());
	m_variantTab.InsertItem(0, ILanguageManager::GetLanguageManager()->GetString(CREATE_NEW_HOUSING_ID).c_str());
	m_variantTab.InsertItem(1, ILanguageManager::GetLanguageManager()->GetString(CHOOSE_HOUSING_ID).c_str());
	m_variantTab.InsertItem(2, ILanguageManager::GetLanguageManager()->GetString(ADD_NAME_HOUSING_ID).c_str());
	m_variantTab.SetCurSel(0);
	m_nameStatic.Create(ILanguageManager::GetLanguageManager()->GetString(HOUSING_NAME_ID).c_str(), WS_CHILD | WS_VISIBLE, CRect(15, 40, 320, 55), this);
	m_nameStatic.SetFont(m_pFont);
	m_name.Create(WS_TABSTOP | WS_CHILD | WS_VISIBLE | WS_BORDER, CRect(15, 55, 320, 75), this, IDC_NAME_HOUSE);
	m_name.SetFont(m_pFont);
	SelChanged();
	m_Ok.EnableWindow(IsEnableOk());
	return bInit;
}

void CAddHousingDialog::SelChanged(){
	if(m_variantTab.GetCurSel() == 0 && m_oldSel)
		SetNew();
	if(m_variantTab.GetCurSel() == 1 && m_oldSel != 1)
		SetChoose();
	if(m_variantTab.GetCurSel() == 2 && m_oldSel != 2)
		SetName();
	m_oldSel = m_variantTab.GetCurSel();
	m_name.SetWindowText(_T(""));
}

void CAddHousingDialog::SetNew(){
	if(m_oldSel == 1)
		m_listHousing.DestroyWindow();
	m_typeHStatic.Create(ILanguageManager::GetLanguageManager()->GetString(TYPE_HOUSING_ID).c_str(), WS_CHILD | WS_VISIBLE, CRect(15, 80, 320, 95), this);
	m_typeHStatic.SetFont(m_pFont);
	m_typeHousing.Create(WS_CHILD | WS_TABSTOP | WS_VISIBLE | CBS_DROPDOWNLIST, CRect(15, 95, 320, 115), this, IDC_TYPEH_EDIT);
	m_typeHousing.SetFont(m_pFont);
	std::map<int, std::tstring> aTypes = IJournalComponent::GetHousingManager()->GetType(Housing::HOUSING);
	for(std::map<int, std::tstring>::iterator it = aTypes.begin(); it != aTypes.end(); it++){
		int cur = m_typeHousing.AddString(it->second.c_str());
		m_typeHousing.SetItemData(cur, it->first);
	}
	m_typeHousing.SetCurSel(0);
	m_typeFStatic.Create(ILanguageManager::GetLanguageManager()->GetString(TYPE_FRAME_ID).c_str(), WS_CHILD | WS_VISIBLE, CRect(15, 120, 320, 135), this);
	m_typeFStatic.SetFont(m_pFont);
	m_typeFrame.Create(WS_CHILD | WS_TABSTOP | WS_VISIBLE | CBS_DROPDOWNLIST, CRect(15, 135, 320, 155), this, IDC_TYPEH_EDIT);
	m_typeFrame.SetFont(m_pFont);aTypes = IJournalComponent::GetHousingManager()->GetType(Housing::FRAME);
	for(std::map<int, std::tstring>::iterator it = aTypes.begin(); it != aTypes.end(); it++){
		int cur = m_typeFrame.AddString(it->second.c_str());
		m_typeFrame.SetItemData(cur, it->first);
	}
	m_typeFrame.SetCurSel(0);
}

void CAddHousingDialog::SetChoose(){
	if(!m_oldSel){
		m_typeFrame.DestroyWindow();
		m_typeHousing.DestroyWindow();
		m_typeHStatic.DestroyWindow();
		m_typeFStatic.DestroyWindow();
	}
	m_listHousing.Create(WS_VISIBLE | WS_CHILD | WS_TABSTOP | WS_BORDER | WS_VSCROLL, CRect(15, 85, 320, 300), this, IDC_LIST_HOUSE);
	std::map<int, CHousingManager::housing> aHousing = IJournalComponent::GetHousingManager()->GetHousings();
	for(std::map<int, CHousingManager::housing>::iterator it = aHousing.begin(); it != aHousing.end(); it++){
		int cur = m_listHousing.AddString(it->second.m_strName.c_str());
		m_listHousing.SetItemData(cur, it->first);
	}
	m_listHousing.SetCurSel(0);
}

void CAddHousingDialog::SetName(){
	if(!m_oldSel){
		m_typeFrame.DestroyWindow();
		m_typeHousing.DestroyWindow();
		m_typeHStatic.DestroyWindow();
		m_typeFStatic.DestroyWindow();
	}
	if(m_oldSel == 1)
		m_listHousing.DestroyWindow();
}


bool CAddHousingDialog::IsEnableOk(){
	if(!m_oldSel || m_oldSel == 2)
		return m_name.GetWindowTextLength();
	return true;
}

void CAddHousingDialog::DoDataExchange(CDataExchange* pDX){
	__super::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_VARIANT_TAB, m_variantTab);
	DDX_Control(pDX, IDOK, m_Ok);
}

void CAddHousingDialog::OnOK(){
	if(m_oldSel == 0){
		CString str;
		m_name.GetWindowText(str);
		CHousingManager::housing hive;
		hive.m_strName = str.GetString();
		hive.m_typeframes = (int)m_typeFrame.GetItemData(m_typeFrame.GetCurSel());
		hive.m_typeHousing = (int)m_typeHousing.GetItemData(m_typeFrame.GetCurSel());
		m_housing.m_nId = IJournalComponent::GetHousingManager()->AddHousing(hive);
		m_housing.m_strName = str.GetString();
	}else if(m_oldSel == 1){
		m_housing.m_nId = (int)m_listHousing.GetItemData(m_listHousing.GetCurSel());
		CString str;
		m_listHousing.GetText(m_listHousing.GetCurSel(), str);
		m_housing.m_strName = str.GetString();
	}else if(m_oldSel == 2){
		CString str;
		m_name.GetWindowText(str);
		m_housing.m_strName = str.GetString();
		m_housing.m_nId = 0;
	}
	EndDialog(IDOK);
}

void CAddHousingDialog::GetHousing(int& nId, std::tstring& strName){
	nId = m_housing.m_nId;
	strName = m_housing.m_strName;
}


BEGIN_MESSAGE_MAP(CAddHousingDialog, CDialog)
	ON_NOTIFY(TCN_SELCHANGE, IDC_VARIANT_TAB, OnTabSelChange)
	ON_CONTROL(EN_CHANGE, IDC_NAME_HOUSE, OnName)
END_MESSAGE_MAP()

void CAddHousingDialog::OnTabSelChange(NMHDR* nmhdr, LRESULT* pResult){
	SelChanged();
}

void CAddHousingDialog::OnName(){
	m_Ok.EnableWindow(IsEnableOk());
	if(m_oldSel == 1){
		CString str;
		m_name.GetWindowText(str);
		int cur = m_listHousing.FindString(0, str);
		if(cur > 0)
			m_listHousing.SetCurSel(cur);
	}
}